<?php
/**    
 * 分组管理
 * @author sun
 * @version 1.0
 * create Date 2013-12-09
 */
class GroupAction extends PublicAction {
	
	/**
	 * 站点分组列表管理
	 */
	public function sitegrouplist(){
		$slist = D('Site')->getSiteListArr('siteconfiglist');
		$sid = intval($_GET['sid']);
		$type = intval($_GET['type']);
		if ($sid > 0) {
			$list = D('Group') -> getGroupList($sid,$type);
			$this->assign('list',$list);
			$this->assign('ssid',$sid);
		}
		$this->assign('type', $type);
		$this->assign('slist', $slist);
		$this->display();
	}
	
	/**
	 * 将站点列表封装成tree
	 * sun
	 */
	public function siteTreeList() {
		echo D('Site')->getSiteList('sitegrouplist');
	}
	
	/**
	 * 分组信息添加/编辑  页面显示
	 */
	public function addGroup(){
		if( !empty($_GET['id']) ){
			$data['id'] =  $_GET['id'];
			$res = model('Group') -> where($data) -> find();
			$this -> assign('res',$res);
		}
		$this->assign('siteid',intval($_GET['sid']));
		$this->assign('type',$_GET['type']);
		$this->display();
	}
	
	/**
	 * 分类信息添加/修改操作
	 */
	public function doaddGroup(){
		$group_name = $_POST['name'];
		if(empty($group_name)){
			$this -> error('分组名称不能为空');
		}
		
		$id = model('Group') -> where("`name` = '{$group_name}' and `type`='{$_POST['type']}'") -> getField('id');
		if(!empty($id) && $id !== $_POST['id']){
			$this -> error('类型名称已存在');
		}
		
		$data['siteid'] = $_POST['siteid'];
		$data['name'] = $_POST['name'];
		$data['description'] = $_POST['description'];
		$data['type'] = $_POST['type'];
		$data['status'] = $_POST['status'];
		if(empty($_POST['id'])){
			$res = model('Group') ->add($data);
		}else {
			$data['id'] = $_POST['id'];
			$res = model('Group') ->save($data);
		}
		if ($res !== false) {
			$this->assign('jumpUrl', URL('cms/Group/sitegrouplist',array('sid'=>intval($_POST['siteid']),'type'=>$_POST['type'])));
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
		
	}
	
	
	/**
	 * 分组信息删除操作
	 */
	public function delGroup(){
		$ids = $_POST['ids'];
		if(empty($ids)){
			echo 0;
			exit;
		}
		
		$map['id'] = array('in',$ids);
		if(model('Group') -> where($map) -> delete()){
			echo 1;
		} else {
			echo 0;
		}
	}
}
?>